package com.ren.permissionapply;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.Permission;
import com.yanzhenjie.permission.PermissionListener;
import com.yanzhenjie.permission.Rationale;
import com.yanzhenjie.permission.RationaleListener;

import java.util.List;

/**
 * 权限申请activity
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_calendarApply;
    private Button btn_cameraApply;
    private Button btn_contactsApply;
    private Button btn_locationApply;
    private Button btn_microphoneApply;
    private Button btn_phoneApply;
    private Button btn_sensorsApply;
    private Button btn_smsApply;
    private Button btn_storageApply;
    private Button btn_moreApply;

    private static final int REQUEST_CODE_SETTING = 300;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_calendarApply = (Button) findViewById(R.id.btn_calendarApply);
        btn_cameraApply = (Button) findViewById(R.id.btn_cameraApply);
        btn_contactsApply = (Button) findViewById(R.id.btn_contactsApply);
        btn_locationApply = (Button) findViewById(R.id.btn_locationApply);
        btn_microphoneApply = (Button) findViewById(R.id.btn_microphoneApply);
        btn_phoneApply = (Button) findViewById(R.id.btn_phoneApply);
        btn_sensorsApply = (Button) findViewById(R.id.btn_sensorsApply);
        btn_smsApply = (Button) findViewById(R.id.btn_smsApply);
        btn_storageApply = (Button) findViewById(R.id.btn_storageApply);
        btn_moreApply = (Button) findViewById(R.id.btn_moreApply);

        btn_calendarApply.setOnClickListener(this);
        btn_cameraApply.setOnClickListener(this);
        btn_contactsApply.setOnClickListener(this);
        btn_locationApply.setOnClickListener(this);
        btn_microphoneApply.setOnClickListener(this);
        btn_phoneApply.setOnClickListener(this);
        btn_sensorsApply.setOnClickListener(this);
        btn_smsApply.setOnClickListener(this);
        btn_storageApply.setOnClickListener(this);
        btn_moreApply.setOnClickListener(this);
    }

    /**
     * 日历权限申请
     */
    private void calendarApply() {
        AndPermission.with(this)
                .permission(Permission.CALENDAR)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 相机权限申请
     */
    private void cameraApply() {
        AndPermission.with(this)
                .permission(Permission.CAMERA)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 通讯录权限申请
     */
    private void cantactsApply() {
        AndPermission.with(this)
                .permission(Permission.CONTACTS)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 位置权限申请
     */
    private void locationApply() {
        AndPermission.with(this)
                .permission(Permission.LOCATION)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 音频录制权限申请
     */
    private void microphoneApply() {
        AndPermission.with(this)
                .permission(Permission.MICROPHONE)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 电话权限申请
     */
    private void phoneApply() {
        AndPermission.with(this)
                .permission(Permission.PHONE)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 传感器权限申请
     */
    private void sensorsApply() {
        AndPermission.with(this)
                .permission(Permission.SENSORS)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 短信权限申请
     */
    private void smsApply() {
        AndPermission.with(this)
                .permission(Permission.SMS)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 外部存储权限申请
     */
    private void storageApply() {
        AndPermission.with(this)
                .permission(Permission.STORAGE)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 多个权限同时申请
     */
    private void moreApply() {
        AndPermission.with(this)
                .permission(Permission.CAMERA, Permission.STORAGE)
                .callback(permissionListener)
                .rationale(new RationaleListener() {
                    @Override
                    public void showRequestPermissionRationale(int requestCode, Rationale rationale) {
                        // 这里的对话框可以自定义，只要调用rationale.resume()就可以继续申请。
                        AndPermission.rationaleDialog(MainActivity.this, rationale).show();
                    }
                })
                .start();
    }

    /**
     * 回调监听。
     */
    private PermissionListener permissionListener = new PermissionListener() {
        @Override
        public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
            Toast.makeText(MainActivity.this, "申请成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
            // 用户否勾选了不再提示并且拒绝了权限，那么提示用户到设置中授权。
            if (AndPermission.hasAlwaysDeniedPermission(MainActivity.this, deniedPermissions)) {
                // 第一种：用默认的提示语。
                AndPermission.defaultSettingDialog(MainActivity.this, REQUEST_CODE_SETTING).show();
            }
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_calendarApply:
                calendarApply();
                break;
            case R.id.btn_cameraApply:
                cameraApply();
                break;
            case R.id.btn_contactsApply:
                cantactsApply();
                break;
            case R.id.btn_locationApply:
                locationApply();
                break;
            case R.id.btn_microphoneApply:
                microphoneApply();
                break;
            case R.id.btn_phoneApply:
                phoneApply();
                break;
            case R.id.btn_sensorsApply:
                sensorsApply();
                break;
            case R.id.btn_smsApply:
                smsApply();
                break;
            case R.id.btn_storageApply:
                storageApply();
                break;
            case R.id.btn_moreApply:
                moreApply();
                break;
        }
    }
}
